#coding=utf-8

from lib.tornado_routes import route
from web.handler import BaseHandler
from tornado import gen
from tornado.web import asynchronous
from decorator.auth import front_login_required
from web.models.user.food.index import Index
# from decorator.auth import admin_login_required, admin_auth
import time


@route(r'/user/food/?', name='user.food')
class IndexHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    @front_login_required
    def get(self):
        school_id = self.get_argument('schoolId', default=self.school_id)

        food, total = yield Index().select_food(school_id, 0, self.offset)

        if (float(total) / self.offset * 10 % 10) > 0:
            total = total / self.offset + 1
        self.render("user/food/index.html", data=food, total=total)


@route(r'/user/food/old?', name='user.food_old')
class OldHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    @front_login_required
    def get(self):
        school_id = self.get_argument('schoolId', default=self.school_id)

        Fstudent_id = self.get_scookie()['user']['Fid']
        food, total = yield Index().select_old_food(school_id, Fstudent_id, 0, self.offset)

        if (float(total) / self.offset * 10 % 10) > 0:
            total = total / self.offset + 1
        self.render("user/food/old.html", data=food, total=total)


@route(r'/user/food/list?', name='user.food_list')
class ListHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    @front_login_required
    def get(self):
        cur_page = self.get_argument('page', default=1)
        school_id = self.get_argument('schoolId', default=self.school_id)

        Fstudent_id = self.get_scookie()['user']['Fid']
        food, total = yield Index().select_food(school_id, int(cur_page) - 1, self.offset)

        if (float(total) / self.offset * 10 % 10) > 0:
            total = total / self.offset + 1

        self.render("user/food/list.html", data=food)

@route(r'/user/food/clist?', name='user.food_clist')
class cListHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    @front_login_required
    def get(self):
        cur_page = self.get_argument('page', default=1)
        school_id = self.get_argument('schoolId', default=self.school_id)

        Fstudent_id = self.get_scookie()['user']['Fid']
        food, total = yield Index().select_old_food(school_id, Fstudent_id, 0, self.offset)

        if (float(total) / self.offset * 10 % 10) > 0:
            total = total / self.offset + 1

        self.render("user/food/clist.html", data=food)

@route(r'/user/food/buy?', name='user.food_buy')
class BuyHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    @front_login_required
    def get(self):
        info = self.get_argument('info', default='')
        school_id = self.get_argument('schoolId', default=self.school_id)

        data = {
            "Fuser_id":self.get_scookie()['user']['Fid'],
            "Ftel":self.get_scookie()['user']['Ftel'],
            "Fname":self.get_scookie()['user']['Fname'],
            "Ftext":info,
            "Fprice":"",
            "Fdorm_id":self.get_scookie()['user']['Fdorm_id'],
            "Froom_id":self.get_scookie()['user']['Froom'],
            "Fschool_id":school_id,
            "Fstatus":0,
            "Fcreate_time":time.time()
        }
        yield Index().buy_food(data)

        self.write('1')